package com.lk.basic.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailCommonService {

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendSimpleMail(String subject,String content,String to){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2042417159@qq.com");
        //邮件主题
        mailMessage.setSubject(subject);
        //邮件内容
        mailMessage.setText(content);
        //收件人
        mailMessage.setTo(to);

        javaMailSender.send(mailMessage);
    }
    public void test2(String subject,String text,String to) throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2042417159@qq.com");
        helper.setSubject(subject);
        http://img30.360buyimg.com/popWaterMark/jfs/t1/67988/7/14094/232759/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg"
        helper.setText(text,true);
        //添加附件
        helper.addAttachment("罗宾.jpg",new File("C:\\Users\\Administrator\\Desktop\\wx.jpg"));
        //helper.addAttachment("压缩文件", new File("C:\\Users\\hm\\Desktop\\20191010\\2020-02-05-智能商贸-DAY4\\resources\\resources.zip"));
        //收件人
        helper.setTo(to);

        javaMailSender.send(mimeMessage);
    }
}
